import unittest
import os

from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy
from BeautifulReport import BeautifulReport  # 导入 BeautifulReport

capabilities = dict(
    platformName='Android',
    automationName='uiautomator2',
    deviceName='Android',
    appPackage='com.android.settings',
    appActivity='.Settings',
    language='en',
    locale='US'
)

appium_server_url = 'http://localhost:4723'

class TestAppium(unittest.TestCase):
    def setUp(self) -> None:
        self.driver = webdriver.Remote(appium_server_url, options=UiAutomator2Options().load_capabilities(capabilities))

    def tearDown(self) -> None:
        if self.driver:
            self.driver.quit()

    def test_find_battery(self) -> None:
        el = self.driver.find_element(by=AppiumBy.XPATH, value='//*[@text="Battery"]')
        el.click()


if __name__ == '__main__':
    report_dir = './reports'
    if not os.path.exists(report_dir):
        os.makedirs(report_dir)
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestAppium))
    runner = BeautifulReport(suite)
    runner.report(
        filename='test_report.html',  # 报告文件名
        description='Appium Test Report',  # 报告描述
        report_dir=report_dir  # 报告保存路径
    )